import 'package:flutter/widgets.dart';
import 'package:flutter_changdian_football/api/api_response.dart';
import 'package:flutter_changdian_football/api/req/club/req_club.dart';
import 'package:flutter_changdian_football/base/base_page_controller.dart';
import 'package:flutter_changdian_football/config/enum/refresh.dart';
import 'package:flutter_changdian_football/model/club/club_rank_model.dart';
import 'package:flutter_changdian_football/pages/home/home_controller.dart';
import 'package:flutter_changdian_football/utils/ext/refresher_extension.dart';
import 'package:pull_to_refresh_flutter3/pull_to_refresh_flutter3.dart';

class ClubRankingController extends BaseGetPageController {

  TextEditingController searchController = TextEditingController();


  List<ClubRankModel> clubRankList = [];

  // ACTIVITY-按活跃度  PLAYER_NUM-俱乐部人数
  String orderType = 'ACTIVITY';

  @override
  void requestData(
    RefreshController controller, 
    {
      Refresh refresh = Refresh.first
    }
  ) async {
    var response = await ReqClub.clubRank(
      pageNum: page,
      keyword: searchController.text,
      pcode: HomeController.to.locationModel?.pcode
    );

    if (response.status == Status.completed) {
      if (response.data['code'] == '200') {
        
        /// 判断是否为最后一页, 如果是， over传true， 否则false
        if (response.data['data']['hasNextPage'] == false) {
          RefreshExtension.onSuccess(controller, refresh, true); 
        } else {
          RefreshExtension.onSuccess(controller, refresh, false);
        }

        /// 下拉刷新需要清除列表
        if (refresh != Refresh.down) {
          clubRankList.clear();
          update();
        }

        for (int i = 0; i < response.data['data']['clubRankVOList'].length; i++) {
          clubRankList.add(ClubRankModel.fromJson(response.data['data']['clubRankVOList'][i]));
        }

        showSuccess(clubRankList);
        update();
      } else if (response.data['code'] == 401) {
        RefreshExtension.onError(controller, refresh);
      } else {
        showError();  
        RefreshExtension.onError(controller, refresh);
      }
    } else {
      showError();  
      RefreshExtension.onError(controller, refresh);
    }
  }

}